<?php
namespace Business\Controller;
use Think\Controller;
class ApiController extends Controller {
	
    public static $session;		//SESSION
	public function _initialize(){
		self::$session=$_SESSION;
		
	}
	//===================
    public function index(){
		$this->display();
    }
	
	
	//把订单设置为洗涤中
	public function order_set(){
		if(IS_POST){
			$orderid=I('orderid');
			$m=M('bb_order');
			if($orderid){
				$set=I('set');
				$order=$m->where(array('orderid'=>$orderid))->find();
				if($order['status']==$set){
					$info['status']=1;
					$info['msg']='yes！';
					$this->ajaxreturn($info);
				}
				if($set){
					$re=$m->where(array('orderid'=>$orderid))->save(array('status'=>$set));
					if($re){
						$info['status']=1;
						$info['msg']='yes！';
					}else{
						$info['status']=0;
						$info['msg']='no！';
					}
				}
	
				$this->ajaxreturn($info);
			}
		}
	}
	
	
	//已洗好通知社区管家送衣
	public function order_song(){
		if(IS_POST){
			$orderid=I('orderid');
			$m=M('bb_order');
			if($orderid){
				$set='5';
				$order=$m->where(array('orderid'=>$orderid))->find();
				if($order['status']==$set){
					$info['status']=1;
					$info['msg']='yes！';
					$this->ajaxreturn($info);
				}
				if($set){
					$re=$m->where(array('orderid'=>$orderid))->save(array('status'=>$set));
					if($re){
						$info['status']=1;
						$info['msg']='yes！';
						
						if($order['auntieid']){
							//推送给阿姨端消息
							$tp=new \bb\template();
							$array=array(
								'name'=>$order['name'],
								'phone'=>$order['phone'],
								'time'=>$order['prckup_dtm'],
								'orderid'=>$order['orderid'],
								'neirong'=>'衣服已洗完'
							);
							$auntie=M('bb_auntie')->where(array('id'=>$order['auntieid'],'shopid'=>$order['shopid']))->find();
							$options['appid']=C('BB_APPID_auntie');
							$options['secret']=C('BB_SECRET_auntie');
							$wx=new \bb\Wechat($options);
							$templatedata=$tp->enddata('put_order',$auntie['openid'],$array);	//组合模板数据
							$wx->sendTemplate($templatedata);	//发送模板
						}
					}else{
						$info['status']=0;
						$info['msg']='no！';
					}
				}
				
			}else{
				$info['status']=0;
				$info['msg']='no！';
			}
			$this->ajaxreturn($info);
		}
	}
}